﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using Newtonsoft.Json;
namespace LandAppraisal.Controllers
{
    public class RegisterController : ApiController
    {
        AdminBLL bll = new AdminBLL();

        #region 获取登记列表
        /// <summary>
        /// 获取登记列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetRegisterList(string option, string optionchild)
        {
            if (option != "请选择" && optionchild != "请选择")
            {
                if (option == "The_Department")
                {
                    var list = bll.RegisterList().Where(p => p.The_Department == optionchild);
                    return JsonConvert.SerializeObject(list);
                }
                if (option == "ProjectNumber")
                {
                    var list = bll.RegisterList().Where(p => p.ProjectNumber == optionchild);
                    return JsonConvert.SerializeObject(list);
                }
                if (option == "Loan_Name")
                {
                    var list = bll.RegisterList().Where(p => p.Loan_Name == optionchild);
                    return JsonConvert.SerializeObject(list);
                }
                if (option == "House_Located")
                {
                    var list = bll.RegisterList().Where(p => p.House_Located == optionchild);
                    return JsonConvert.SerializeObject(list);
                }
            }
            return JsonConvert.SerializeObject(bll.RegisterList());
        }
        #endregion

        #region 添加到客户表中
        /// <summary>
        /// 添加到客户表中
        /// </summary>
        /// <param name="eal"></param>
        /// <returns></returns>
        [HttpPost]
        public int Post([FromBody]EALoan_Info eal)
        {
            eal.CreateTime = DateTime.Now;
            var name = eal.Loan_Name;
            var tel = eal.Loan_TEL;
            var id = eal.Loan_IdCard;
            return bll.Add<EALoan_Info>(eal);
        }
        #endregion

        #region 添加到基本信息表中
        /// <summary>
        /// 添加到基本信息表中
        /// </summary>
        /// <param name="addregister"></param>
        /// <returns></returns>
        [HttpPost]
        public int AddEABasicInfo([FromBody]AddRegister addregister)
        {
            //客户信息
            EALoan_Info eali = new EALoan_Info();
            eali.Loan_Name = addregister.Loan_Name;
            eali.Loan_IdCard = addregister.Loan_IdCard;
            eali.Loan_TEL = addregister.Loan_TEL;
            eali.Loan_Contacts = addregister.Loan_Contacts;
            eali.Loan_ContactsTEL = addregister.Loan_ContactsTEL;
            eali.CreateTime = DateTime.Now;
            eali.Create_People = addregister.Create_People;
            //基本信息
            EABasic_Info eabi = new EABasic_Info();
            eabi.Basic_CreateTime = DateTime.Now;
            eabi.Issue_Time = addregister.Issue_Time;
            eabi.Evaluation = addregister.Evaluation;
            eabi.Survey = addregister.Survey;
            eabi.The_Department = addregister.The_Department;
            eabi.Assessment = addregister.Assessment;
            eabi.Presentation_Type = addregister.Presentation_Type;
            eabi.Yers_Source = addregister.Yers_Source;
            eabi.House_Time = addregister.House_Time;
            eabi.House_Purpose = addregister.House_Purpose;
            eabi.House_Located = addregister.House_Located;
            eabi.Basic_Quota = addregister.Basic_Quota;
            eabi.Surplus_Years = addregister.Surplus_Years;
            eabi.Basic_State = 1;

            var LastProjectNumber = bll.SelectProjectNumber();//获取数据库中最后一次添加的项目号
            if (LastProjectNumber != "")//存在项目号
            {
                var ProjectNumber = Assignment(LastProjectNumber);
                eali.ProjectNumber = ProjectNumber;//客户信息表中的项目号
                eabi.ProjectNumber = ProjectNumber;//基本信息表中的项目号
                if (bll.Add<EALoan_Info>(eali) > 0 && bll.Add<EABasic_Info>(eabi) > 0)
                {
                    return 1;//添加成功
                }
                else
                {
                    return 0;//添加失败
                }
            }
            else
            {//数据库中没有项目号
                var Year = DateTime.Now.Year.ToString();
                var Month = DateTime.Now.Month.ToString();
                var str = Year + Month + "000";
                var ProjectNumber = Assignment(str);
                eali.ProjectNumber = ProjectNumber;//客户信息表中的项目号
                eabi.ProjectNumber = ProjectNumber;//基本信息表中的项目号
                if (bll.Add<EALoan_Info>(eali) > 0 && bll.Add<EABasic_Info>(eabi) > 0)
                {
                    return 1;//添加成功
                }
                else
                {
                    return 0;//添加失败
                }
            }
        }
        /// <summary>
        /// 生成项目号
        /// </summary>
        /// <param name="LastNumStr"></param>
        /// <returns></returns>
        public static String Assignment(String LastNumStr)
        {
            string number0 = "";
            DateTime date = System.DateTime.Now;
            string year = date.Year.ToString();
            string month = date.Month.ToString();
            string day = date.Day.ToString();
            if (month.Length < 2)
                month = '0' + month;
            if (day.Length < 2)
                day = '0' + day;
            string ymd = year + month + day;
            if (LastNumStr.Length < 8 || LastNumStr.Substring(0, 8) != ymd || LastNumStr.Substring(4, 2) != month || LastNumStr.Substring(0, 4) != year)
            {
                var list = LastNumStr.Substring(6, 2);

                return ymd + "0001";
            }
            else
            {

                Int32 clientnumber = Convert.ToInt32(LastNumStr.Substring(8, LastNumStr.Length - 8)) + 1;
                if (clientnumber.ToString().Length > LastNumStr.Length - 8)
                {
                    return ymd + clientnumber.ToString();
                }
                else
                {
                    for (int i = 0; i < LastNumStr.Length - 8 - clientnumber.ToString().Length; i++)
                    {
                        number0 += "0";
                    }
                    return ymd + number0 + clientnumber.ToString();
                }
            }
        }

        #endregion

        #region 获取基础表中测所有的项目号
        /// <summary>
        ///获取基础表中测所有的项目号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetProjectNumber()
        {
            return JsonConvert.SerializeObject(bll.RegisterList());
        }
        #endregion

        #region 借款人
        /// <summary>
        ///借款人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetLoanName()
        {
            return JsonConvert.SerializeObject(bll.GetList<EALoan_Info>());
        }
        #endregion

        #region 房屋坐落
        /// <summary>
        ///房屋坐落
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetHouseLocated()
        {
            return JsonConvert.SerializeObject(bll.GetList<EABasic_Info>());
        }
        #endregion


    }
}
